#include "DBManger.h"
#include <FS.h>
#include <LittleFS.h>

const char *DB_FILENAME PROGMEM = "/config.json";

DBManger::DBManger(/* args */)
    : jdoc(512)
{
    LittleFS.begin();
    // SPIFFS.begin();
}

DBManger::~DBManger()
{
    LittleFS.end();
    // SPIFFS.end();
}

void DBManger::init()
{
    load();
}

void DBManger::load()
{
    File f = LittleFS.open(DB_FILENAME, "r");
    if (!f)
    {
        Serial.println("file open failed");
    }
    else
    {
        DeserializationError error = deserializeJson(jdoc, f);
        if (error)
            Serial.printf_P("Failed to read file, using default configuration %s \r\n", error.c_str());
    }

    f.close();
}

void DBManger::save()
{
    File f = LittleFS.open(DB_FILENAME, "w+");
    if (!f)
    {
        Serial.println("file read failed");
        return;
    }

    if (serializeJsonPretty(jdoc, f) == 0)
        Serial.println(F("Failed to write to file"));

    // serializeJsonPretty(jdoc, Serial);
    f.close();
}

JsonArray DBManger::getWiFiEntry()
{
    JsonArray wlan = jdoc["wlan"].as<JsonArray>();
    if (wlan.isNull())
        wlan = jdoc.createNestedArray("wlan");

    if (wlan.size() <= 0)
    {
        JsonObject wlan_0 = wlan.createNestedObject();
        wlan_0["ssid"] = "GDCN_1102";
        wlan_0["passphrase"] = "kktt0219";

        JsonObject wlan_1 = wlan.createNestedObject();
        wlan_1["ssid"] = "MERCURY_1102";
        wlan_1["passphrase"] = "kktt0219";

        JsonObject wlan_2 = wlan.createNestedObject();
        wlan_2["ssid"] = "nthuanyu";
        wlan_2["passphrase"] = "NT8919111";
    }
    return wlan;
}

JsonArray DBManger::getLightEntry()
{
    JsonArray timeon = jdoc["timeon"].as<JsonArray>();

    if (timeon.isNull())
        timeon = jdoc.createNestedArray("timeon");

    if (timeon.size() <= 0)
    {
        JsonObject timeon_0 = timeon.createNestedObject();
        timeon_0["s"] = 8;
        timeon_0["e"] = 10;

        JsonObject timeon_1 = timeon.createNestedObject();
        timeon_1["s"] = 17;
        timeon_1["e"] = 23;
    }

    return timeon;
}

int DBManger::getLampType()
{
    int lamp = jdoc["lamp"] | 2;
    return lamp;
}

void DBManger::setLampType(int type)
{
    jdoc["lamp"] = type;
    save();
}

void DBManger::loadLampValue(int16_t (*lightVal)[4])
{

    JsonArray lamp_level = jdoc["lamp_level"].as<JsonArray>();
    if (lamp_level.isNull())
    {
        lamp_level = jdoc.createNestedArray("lamp_level");
        lamp_level.add(0);
        lamp_level.add(1023);
        lamp_level.add(1023);
        lamp_level.add(1023);
    }

    (*lightVal)[0] = lamp_level[0] | 0;
    (*lightVal)[1] = lamp_level[1] | 1023;
    (*lightVal)[2] = lamp_level[2] | 1023;
    (*lightVal)[3] = lamp_level[3] | 1023;
}

void DBManger::setLampValue(int16_t lightVal[4])
{
    JsonArray lamp_level = jdoc["lamp_level"].as<JsonArray>();
    if (lamp_level.isNull())
        lamp_level = jdoc.createNestedArray();

    lamp_level[0] = lightVal[0];
    lamp_level[1] = lightVal[1];
    lamp_level[2] = lightVal[2];
    lamp_level[3] = lightVal[3];

    save();
}